//
//  NSObject+HBPopCategory.m
//  HBActionSheet
//
//  Created by 韩冰 on 2017/10/23.
//  Copyright © 2017年 韩冰. All rights reserved.
//

#import "HBPopCategory.h"
#import "HBWindow.h"
#import "HBCommon.h"
#import <objc/runtime.h>

static const void *HB_dimReferenceCountKey            = &HB_dimReferenceCountKey;
static const void *HB_dimBackgroundViewKey            = &HB_dimBackgroundViewKey;
static const void *HB_dimAnimationDurationKey         = &HB_dimAnimationDurationKey;
static const void *HB_dimBackgroundAnimatingKey       = &HB_dimBackgroundAnimatingKey;

static const void *HB_dimBackgroundBlurViewKey        = &HB_dimBackgroundBlurViewKey;
static const void *HB_dimBackgroundBlurEnabledKey     = &HB_dimBackgroundBlurEnabledKey;
static const void *HB_dimBackgroundBlurEffectStyleKey = &HB_dimBackgroundBlurEffectStyleKey;


@interface UIView (HBPopupInner)

@property (nonatomic, assign, readwrite) NSInteger HB_dimReferenceCount;

@end

@implementation UIView (HBPopupInner)

@dynamic HB_dimReferenceCount;


- (NSInteger)HB_dimReferenceCount {
    return [objc_getAssociatedObject(self, HB_dimReferenceCountKey) integerValue];
}

- (void)setHB_dimReferenceCount:(NSInteger)HB_dimReferenceCount
{
    objc_setAssociatedObject(self, HB_dimReferenceCountKey, @(HB_dimReferenceCount), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end


@implementation UIView (HBPopup)

@dynamic HB_dimBackgroundView;
@dynamic HB_dimAnimationDuration;
@dynamic HB_dimBackgroundAnimating;
@dynamic HB_dimBackgroundBlurView;


- (UIView *)HB_dimBackgroundView
{
    UIView *dimView = objc_getAssociatedObject(self,HB_dimBackgroundViewKey);
    
    if ( !dimView )
    {
        dimView = [UIView new];
        [self addSubview:dimView];
        dimView.frame = self.bounds;
        dimView.alpha = 0.0f;
        dimView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        dimView.layer.zPosition = FLT_MAX;
        self.HB_dimAnimationDuration = 0.3f;
        objc_setAssociatedObject(self, HB_dimBackgroundViewKey, dimView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    return dimView;
}
//HB_dimBackgroundBlurEnabled
- (BOOL)HB_dimBackgroundBlurEnabled
{
    return [objc_getAssociatedObject(self, HB_dimBackgroundBlurEnabledKey) boolValue];
}

- (void)setHB_dimBackgroundBlurEnabled:(BOOL)HB_dimBackgroundBlurEnabled
{
    objc_setAssociatedObject(self, HB_dimBackgroundBlurEnabledKey, @(HB_dimBackgroundBlurEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if ( HB_dimBackgroundBlurEnabled )
    {
        self.HB_dimBackgroundView.backgroundColor = [UIColor whiteColor];
        self.HB_dimBackgroundBlurEffectStyle = self.HB_dimBackgroundBlurEffectStyle;
        self.HB_dimBackgroundBlurView.hidden = NO;
    }
    else
    {
        self.HB_dimBackgroundView.backgroundColor =[UIColor whiteColor];
        self.HB_dimBackgroundBlurView.hidden = YES;
    }
}
- (UIBlurEffectStyle)HB_dimBackgroundBlurEffectStyle
{
    return [objc_getAssociatedObject(self, HB_dimBackgroundBlurEffectStyleKey) integerValue];
}

- (void)setHB_dimBackgroundBlurEffectStyle:(UIBlurEffectStyle)HB_dimBackgroundBlurEffectStyle
{
    objc_setAssociatedObject(self, HB_dimBackgroundBlurEffectStyleKey, @(HB_dimBackgroundBlurEffectStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if ( self.HB_dimBackgroundBlurEnabled )
    {
        [self.HB_dimBackgroundBlurView removeFromSuperview];
        self.HB_dimBackgroundBlurView = nil;
        
        UIView *blurView = [self HB_dimBackgroundBlurView];
        [self.HB_dimBackgroundView addSubview:blurView];
        blurView.frame = self.HB_dimBackgroundView.bounds;

    }
}

//mm_dimBackgroundBlurView
- (UIView *)HB_dimBackgroundBlurView
{
    UIView *blurView = objc_getAssociatedObject(self, HB_dimBackgroundBlurViewKey);
    
    if ( !blurView )
    {
        blurView = [UIView new];
        
        if ( [UIVisualEffectView class] )
        {
            UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:self.HB_dimBackgroundBlurEffectStyle]];
            [blurView addSubview:effectView];
            effectView.frame = blurView.frame;
        }
        else
        {
            blurView.backgroundColor = @[[UIColor orangeColor],[UIColor orangeColor],[UIColor orangeColor]][self.HB_dimBackgroundBlurEffectStyle];
        }
        blurView.userInteractionEnabled = NO;
        
        objc_setAssociatedObject(self, HB_dimBackgroundBlurViewKey, blurView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    return blurView;
}

- (void)setHB_dimBackgroundBlurView:(UIView *)HB_dimBackgroundBlurView
{
    objc_setAssociatedObject(self, HB_dimBackgroundBlurViewKey, HB_dimBackgroundBlurView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//mm_dimBackgroundAnimating
- (BOOL)HB_dimBackgroundAnimating
{
    return [objc_getAssociatedObject(self, HB_dimBackgroundAnimatingKey) boolValue];
}

- (void)setHB_dimBackgroundAnimating:(BOOL)HB_dimBackgroundAnimating
{
    objc_setAssociatedObject(self, HB_dimBackgroundAnimatingKey, @(HB_dimBackgroundAnimating), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//mm_dimAnimationDuration
- (NSTimeInterval)HB_dimAnimationDuration
{
    return [objc_getAssociatedObject(self, HB_dimAnimationDurationKey) doubleValue];
}

- (void)setHB_dimAnimationDuration:(NSTimeInterval)HB_dimAnimationDuration
{
    objc_setAssociatedObject(self, HB_dimAnimationDurationKey, @(HB_dimAnimationDuration), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)HB_showDimBackground
{
    ++self.HB_dimReferenceCount;
    
    if ( self.HB_dimReferenceCount > 1 )
    {
        return;
    }
    
    self.HB_dimBackgroundView.hidden = NO;
    self.HB_dimBackgroundAnimating = YES;
    
    if ( self == [HBWindow sharedWindow].attachView )
    {
        [HBWindow sharedWindow].hidden = NO;
        [[HBWindow sharedWindow] makeKeyAndVisible];
    }
    else if ( [self isKindOfClass:[UIWindow class]] )
    {
        self.hidden = NO;
        [(UIWindow*)self makeKeyAndVisible];
    }
    else
    {
        [self bringSubviewToFront:self.HB_dimBackgroundView];
    }
    
    [UIView animateWithDuration:self.HB_dimAnimationDuration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         
                         self.HB_dimBackgroundView.alpha = 1.0f;
                         
                     } completion:^(BOOL finished) {
                         
                         if ( finished )
                         {
                             self.HB_dimBackgroundAnimating = NO;
                         }
                         
                     }];
}

- (void)HB_hideDimBackground
{
    --self.HB_dimReferenceCount;
    
    if ( self.HB_dimReferenceCount > 0 )
    {
        return;
    }
    
    self.HB_dimBackgroundAnimating = YES;
    [UIView animateWithDuration:self.HB_dimAnimationDuration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         
                         self.HB_dimBackgroundView.alpha = 0.0f;
                         
                     } completion:^(BOOL finished) {
                         
                         if ( finished )
                         {
                             self.HB_dimBackgroundAnimating = NO;
                             
                             if ( self == [HBWindow sharedWindow].attachView )
                             {
                                 [HBWindow sharedWindow].hidden = YES;
                                 [[[UIApplication sharedApplication].delegate window] makeKeyWindow];
                             }
                             else if ( self == [HBWindow sharedWindow] )
                             {
                                 self.hidden = YES;
                                 [[[UIApplication sharedApplication].delegate window] makeKeyWindow];
                             }
                         }
                     }];
}

- (void) HB_distributeSpacingHorizontallyWith:(NSArray*)views
{
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( int i = 0 ; i < views.count+1 ; ++i )
    {
        UIView *v = [UIView new];
        [spaces addObject:v];
        [self addSubview:v];
        v.width =   v.height;
    }
    
    UIView *v0 = spaces[0];
    v0.left=  self.left ;
    v0.centerY =  ((UIView*)views[0]).centerY ;
    
    UIView *lastSpace = v0;
    for ( int i = 0 ; i < views.count; ++i )
    {
        UIView *obj = views[i];
        UIView *space = spaces[i+1];
        obj.left = lastSpace.right;
        space.left = obj.right;
        space.centerY = obj.centerY;
        space.width = v0.width;
        lastSpace = space;
    }
    lastSpace.right = self.right;
    
}

- (void) HB_distributeSpacingVerticallyWith:(NSArray*)views
{
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( int i = 0 ; i < views.count+1 ; ++i )
    {
        UIView *v = [UIView new];
        [spaces addObject:v];
        [self addSubview:v];
        v.width = v.height;
    }
    
    UIView *v0 = spaces[0];
    v0.top = self.top;
    v0.centerX =((UIView*)views[0]).centerX;
    
    UIView *lastSpace = v0;
    for ( int i = 0 ; i < views.count; ++i )
    {
        UIView *obj = views[i];
        UIView *space = spaces[i+1];
        obj.top = lastSpace.bottom;
        space.top = obj.bottom;
        space.centerX = obj.centerX;
        space.height = v0.height;
        lastSpace = space;
    }
    lastSpace.bottom = self.bottom;    
}


@end
